﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;


public partial class InventoryTracker_LocationMaster1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnNew.Enabled=true;
        btnDelete.Enabled=false;
        btnEdit.Enabled = false;
        btnLocSubmit.Enabled = true;
        btnLocUpdate.Enabled = false;
    }
    protected void btnLocSubmit_Click(object sender, EventArgs e)
    {
        if (txtLocAddressLine2.Text == null || txtLocAddressLine2.Text == "")
        {
            txtLocAddressLine2.Text = "-";
        }
        if (txtLocPhone.Text == null || txtLocPhone.Text == "")
        {
            txtLocPhone.Text = "0";
        }
        if (txtLocFax.Text == null || txtLocFax.Text == "")
        {
            txtLocFax.Text = "0";
        }
        if (txtLocRemarks.Text == null || txtLocRemarks.Text == "")
        {
            txtLocRemarks.Text = "-";
        }


                //ReqFieldValiPhone.Enabled = true;
        DataAccess DA = DataAccessHelper.GetDataAccess();
        Location aLocation = new Location(txtLocName.Text,txtLocAddressLine1.Text,txtLocAddressLine2.Text,txtLocCity.Text,txtLocState.Text,txtLocCountry.Text,Int64.Parse(txtLocPhone.Text),Int64.Parse( txtLocFax.Text),txtLocRemarks.Text);
        try
        {
            if (DA.CreateLocation(aLocation))
            {
                lblResult.Text = "Success";
            }
            else
            {
                lblResult.Text = "Failure";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message.ToString();
        }
    }

    protected void DdlLocationName_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnNew.Enabled = true;
        btnDelete.Enabled = true;
        btnEdit.Enabled = true;
        btnLocSubmit.Enabled = false;
        btnLocUpdate.Enabled = false;

        String SelectValue = DdlLocationName.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM  LocationMaster WHERE  [LocationID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        txtLocName.Text = dataTable.Rows[0]["LocationName"].ToString(); 
        txtLocAddressLine1.Text = dataTable.Rows[0]["LocAddr1"].ToString();
        txtLocAddressLine2.Text = dataTable.Rows[0]["LocAddr2"].ToString();
        txtLocCity.Text = dataTable.Rows[0]["LocCity"].ToString();
        txtLocState.Text = dataTable.Rows[0]["LocState"].ToString();
        txtLocCountry.Text = dataTable.Rows[0]["LocCountry"].ToString();
        txtLocPhone.Text = txtLocCity.Text = dataTable.Rows[0]["LocPhone"].ToString();
        txtLocFax.Text = dataTable.Rows[0]["LocFax"].ToString();
        txtLocRemarks.Text = dataTable.Rows[0]["Remarks"].ToString();
        con.Close();


    }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }


    protected void btnNew_Click(object sender, EventArgs e)
    {
        //ReqFieldValiPhone.Enabled = false;
        btnNew.Enabled = false;
        btnDelete.Enabled = false;
        btnEdit.Enabled = false;
        btnLocSubmit.Enabled = true;
        btnLocUpdate.Enabled = false;

        txtLocName.Text = "";
        txtLocAddressLine1.Text = "";
        txtLocAddressLine2.Text = "";
        txtLocCity.Text = "";
        txtLocState.Text = "";
        txtLocCountry.Text = "";
        txtLocPhone.Text = "";
        txtLocFax.Text = "";
        txtLocRemarks.Text = "";

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        String SelectValue = DdlLocationName.SelectedValue;
        btnNew.Enabled = false;
        btnDelete.Enabled = true;
        btnEdit.Enabled = false;
        btnLocSubmit.Enabled = false;
        btnLocUpdate.Enabled = false;


        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM  LocationMaster WHERE  [LocationID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtLocName.Text = "";
        txtLocAddressLine1.Text = "";
        txtLocAddressLine2.Text = "";
        txtLocCity.Text = "";
        txtLocState.Text = "";
        txtLocCountry.Text = "";
        txtLocPhone.Text = "";
        txtLocFax.Text = "";
        txtLocRemarks.Text = "";

    }
    
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        btnNew.Enabled = false;
        btnDelete.Enabled = false;
        btnEdit.Enabled = false;
        btnLocSubmit.Enabled = false;
        btnLocUpdate.Enabled = true;

       

        

    }
    protected void btnLocUpdate_Click(object sender, EventArgs e)
    {
        btnNew.Enabled = false;
        btnDelete.Enabled = false;
        btnEdit.Enabled = false;
        btnLocSubmit.Enabled = false;
        btnLocUpdate.Enabled = true;

        String SelectValue = DdlLocationName.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE LocationMaster SET  LocationName = '" + txtLocName.Text + "' , LocAddr1 = '" + txtLocAddressLine1.Text + "' , LocAddr2 ='" + txtLocAddressLine2.Text + "', LocCity ='" + txtLocCity.Text + "' , LocState ='" + txtLocState.Text + "' , LocCountry ='" + txtLocCountry.Text + "', LocPhone = '" + txtLocPhone.Text + "', LocFax = '" + txtLocFax.Text + "', Remarks = '" + txtLocRemarks.Text + "' WHERE [LocationID]=" + SelectValue;
        //String sql= "UPDATE LocationMaster SET LocationName = @LocationName, LocAddr1 = @LocAddr1, LocAddr2 = @LocAddr2, LocCity = @LocCity, LocState = @LocState, LocCountry = @LocCountry, LocPhone = @LocPhone, LocFax = @LocFax, Remarks = @Remarks WHERE [LocationID] = " + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtLocName.Text = "";
        txtLocAddressLine1.Text = "";
        txtLocAddressLine2.Text = "";
        txtLocCity.Text = "";
        txtLocState.Text = "";
        txtLocCountry.Text = "";
        txtLocPhone.Text = "";
        txtLocFax.Text = "";
        txtLocRemarks.Text = "";
    }
}
